//
//  LRNewfeatureController.swift
//  LRWeibo
//
//  Created by 雷路荣 on 15/12/17.
//  Copyright © 2015年 leilurong. All rights reserved.
//

import UIKit
import SDWebImage

class LRWelcomeController: UIViewController {

    // MARK: - 属性
    /// iconView底部约束
    private var iconImageViewBottomConstraint: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 1. 加载子控件
        prepareUI()
        // 2. 先显示上一次保存的头像
        setIconView()
        // 3. 加载最新的用户头像
        LRUserAccount.loadUserAccount()?.loadUserInfo({ (error) -> () in
            // 判断是否有错误
            if error != nil {
                print("error=\(error)")
                return
            }
            // 获取用户数据没有出错,加载最新的头像
            self.setIconView()
        })
    }
    // 显示当前用户头像
    func setIconView() {
        if let avatar_large = LRUserAccount.loadUserAccount()?.avatar_large {
            //获取头像的url
            let url = NSURL(string: avatar_large)
            print("头像 url: \(url)")
            //下载显示头像
            self.iconImageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        iconImageViewBottomConstraint?.constant -= self.view.bounds.height*0.55
        //弹性动画
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
//            self.iconImageView.center.y -= self.view.bounds.height*0.55
//            self.welcomeLabel.center.y -= self.view.bounds.height*0.55
            self.view.layoutIfNeeded()
            }) { (_) -> Void in //“_” 不关注这值
                self.welcomeLabel.hidden = false
                self.welcomeLabel.alpha = 0
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //切换界面，跳转到主界面
                        AppDelegate.outSwitchRootViewController(true)
                })
         }
    }
    //加载子控件
    private func prepareUI(){
        //添加子控件
        view.addSubview(bgImageView)
        view.addSubview(iconImageView)
        view.addSubview(welcomeLabel)
        //设置约束
//        bgImageView.frame = view.bounds;
//        iconImageView.center = CGPointMake(view.bounds.size.width*0.5, view.bounds.size.height*0.7)
//        welcomeLabel.center = CGPointMake(iconImageView.center.x, CGRectGetMaxY(iconImageView.frame)+18)
        
        //背景约束填充整个view
        bgImageView.ff_Fill(view)
        //头像约束
        let constraint = iconImageView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSizeMake(85, 85), offset: CGPointMake(0, -160))
        //欢迎标签约束
        welcomeLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconImageView, size: nil, offset: CGPointMake(0, 16))
        //记录底部约束
        iconImageViewBottomConstraint = iconImageView.ff_Constraint(constraint, attribute: NSLayoutAttribute.Bottom)
        
    }
    //背景图片
    private lazy var bgImageView = UIImageView(image: UIImage(named: "ad_background"))
    //头像
    private lazy var iconImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        return imageView;
    }()
    //欢迎归来
    private lazy var welcomeLabel: UILabel = {
        let label = UILabel()
        label.text = "欢迎归来"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(18)
        label.textAlignment = NSTextAlignment.Center;
        label.sizeToFit()
        //一开始先隐藏
        label.hidden = true
        return label
    }()

}
